package com.bainuo.doctor.common.base;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bainuo.doctor.common.util.ImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Description:与normal adapter进行配合使用
 * Created by MonkeyShen on 2017/1/26.
 * Mail:shenminjie92@sina.com
 */

public class NormalViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private View mConvertView;

    private NormalViewHolder(View v) {
        super(v);
        mConvertView = v;
        mViews = new SparseArray<>();
    }

    public static NormalViewHolder get(ViewGroup parent, int layoutId) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        return new NormalViewHolder(convertView);
    }

    /**
     * 使用泛型获取view
     *
     * @param id  id
     * @param <T> <T>
     * @return
     */
    public <T extends View> T getView(int id) {
        View v = mViews.get(id);
        if (v == null) {
            v = mConvertView.findViewById(id);
            mViews.put(id, v);
        }
        return (T) v;
    }

    public View getItemView() {
        return mConvertView;
    }

    /**
     * 设置文本
     *
     * @param id
     * @param value
     */
    public void setText(int id, String value) {
        TextView view = getView(id);
        view.setText(value);
    }

    /**
     * 设置图片(只能使用fresco)
     *
     * @param id
     * @param uriString
     */
    public void setImage(int id, String uriString) {
        SimpleDraweeView simpleDraweeView = getView(id);
        ImageLoader.setImage(uriString,simpleDraweeView);
    }


}
